layui.use(['form','table','laypage'], function(){
  var table = layui.table;
  var form = layui.form,
	layer = layui.layer;
	/*初始化表格*/
	function tableInit(where = {})
	{  
	  table.render({
		elem: '#dataTables-example'
		,url:'/admin/goods/goodslist.html'
		,where:where
		,method:'POST'
		,parseData: function(res){ //res 即为原始返回的数据
			//封装返回数据
			console.log(res);
			var urls = '';
            for (i in res.data) {
                urls = '/admin/goods/stocklist/id/'+res.data[i]['goods_id']+'.html';
                res.data[i]['reason'] = "<button class='layui-btn' onclick=\"xadmin.open('查看库存','"+urls+"',800,500)\">查看库存</button>";;
                urls = '/admin/Supplier/editsupplier/id/'+res.data[i]['goods_id']+'.html';
                res.data[i]['reason'] += "<button class='layui-btn' onclick=\"xadmin.open('历史进阶','"+urls+"',800,500)\">历史进阶</button>";
            }
            
			return {
			  "code": 10001, //解析接口状态
			  "msg": '', //解析提示文本
			  "count": res.total, //解析数据长度
			  "data": res.data //解析数据列表
			};
		}
		,cols: [[
		  {field:'goods_id', title: '序号',width:50}
		  ,{field:'goods_sn', title: '产品编码'} 
		  ,{field:'goods_name', title: '产品名称'}
		  ,{field:'number', title: '数量',width:70}
		  ,{field:'unit', title: '单位',width:50}
		  ,{field:'selling_price', title: '零售单价（元）',width:120}
		  ,{field:'trade_price', title: '批发价（元）',width:110}
		  ,{field:'warning_overdue', title: '过期提醒（天）',width:120}
		  ,{field:'warning_number', title: '产品预警',width:80}
		  ,{field:'profit_bonus', title: '产品提成（%）',width:120}
		  ,{field:'reason', title: '操作'}
		]]
		,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
		  layout: ['prev', 'page', 'next', 'skip', 'count']
		}
		,response: {
			statusCode: 10001
		}
	  });
	}
	tableInit();
	 
	//监听提交
	form.on('submit(sreach)',
	function(data) {
		tableInit(data.field);
		return false;
	});
});

